﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.UI.Controls;
using Medianamik.UI.Web.MVP.View;
using Telerik.Web.UI;

namespace Medianamik.UI.Web.Shop
{
    public partial class ShopBrands : ModuleViewWithGrid<IBrands>, IBrands
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FilterButton.Click += OnFilterButton;

            InitModalWindows();
        }

        void OnFilterButton(object sender, EventArgs e)
        {
            OnFilter();
        }

        public override IMedianamikGrid Grid
        {
            get { return (IMedianamikGrid)BrandGrid; }
        }

        public override IEnumerable<ListItem> Status
        {
            get { return ContentStatesList.Items.Cast<ListItem>(); }
            set
            {
                ContentStatesList.Items.AddRange(value.ToArray());
            }
        }

        public override DateTime? SelectedStartDateTime
        {
            get { return null; }
            set { }
        }

        public override DateTime? SelectedEndDateTime
        {
            get { return null; }
            set { }
        }

        public override string Keywords
        {
            get { return Name.Text; }
            set { Name.Text = value; }
        }

        public override string ListingPageUrl
        {
            get { return Pages.BrandList; }
        }

        public override string ChildrenListingPageUrl
        {
            get
            {
                return ListingPageUrl;
            }
        }

        public override Guid BaseTypeId
        {
            get
            {
                return MedianamikTypes.BaseType;
            }
        }

        public override string EditPageUrl
        {
            get
            {
                return Pages.Edit;
            }
        }

        public override string BreadcrumbTrail
        {
            get { return Breadcrumbs.Text; }
            set { Breadcrumbs.Text = value; }
        }

        public override Guid ModuleNodeId
        {
            get { return Modules.Shop; }
        }

        public override Guid? ParentNodeId
        {
            get
            {
                return Request.QueryString.GetValue<Guid?>(DefaultParamNames.ParentNodeID) ?? PageIds.BrandList;
            }
        }

        public override bool IsMultiLingual
        {
            get
            {
                return true;
            }
        }

        private void InitModalWindows()
        {
            var typeSelectorWindow = new BaseModalWindow { ID = "TypeSelectorWindow" };

            if (!Page.IsPostBack)
            {
                typeSelectorWindow.OnClientClose = "onNodeTypeSelected";
                typeSelectorWindow.IconUrl = "/Medianamik/images/Icones/Ico_AjoutPage.gif";
                typeSelectorWindow.ReloadOnShow = true;
                typeSelectorWindow.Width = Unit.Pixel(400);
                typeSelectorWindow.Height = Unit.Pixel(200);
                typeSelectorWindow.Behaviors = WindowBehaviors.Close;
            }

            MyWindowManager.Windows.Add(typeSelectorWindow);

            lnkAddNode.NavigateUrl = String.Format("/medianamik/modalwindows/typeselectorwindow.aspx{0}", GetParentNodeParam("?"));
            lnkAddNode.Attributes.Add("onClick", "GetRadWindowManager().Open(null, 'TypeSelectorWindow');");
        }

        protected string GetParentNodeParam(string pParamDelimiter)
        {
            return String.Format("{0}{2}={1}", pParamDelimiter, ParentNodeId, DefaultParamNames.ParentNodeID);
        }
    }

    public interface IBrands : IModuleViewWithGrid
    {
    }
}
